import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import time

# 房屋地址，房屋价格，图片地址，下载图片
url = 'https://hf.zu.fang.com/'
driver = webdriver.PhantomJS(service_args=['--load-images=no'])
driver.get(url)
driver.find_element_by_id('input_key').send_keys('蜀山')
driver.find_element_by_id('rentid_39').click()

page = driver.page_source
soup = BeautifulSoup(page,'lxml')
pic_list = soup.find_all(class_ = 'list hiddenMap rel')
for i in range(1,6):
    print('正在抓取第%d页' % i)
    for pic_info in pic_list:
    # 图片地址
        img_url = 'https:' + pic_info.dt.a.img['data-src']
        print(img_url)
        # 位置信息
        title = pic_info.find(class_ = 'gray6 mt12').get_text(strip=True)
        #print(title)
        # 价格信息
        price = pic_info.find(class_ = 'mt5 alingC').get_text(strip=True).replace('/','-')
        #print(price)
        name = title +'-' + price
        #print(name)

        img = requests.get(img_url).content

        with open('爬虫\\pics\\{}.jpg'.format(name),'wb') as f:
            f.write(img)
        
    driver.find_element_by_link_text('下一页').click()

print('抓取完毕')